#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# A shell and stripped down version version of the crazy-small-volume-test.py
#
#
TestName="crazy-smaller-vol-test"
JobName=Simple
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
mkdir -p ${tmpsrc}
dd if=/dev/urandom of=${tmpsrc}/the_file bs=1M count=20
the_file_md5=`md5sum ${tmpsrc}/the_file | cut -d " " -f 1`
echo $the_file_md5
cd ${cwd}

echo ${tmpsrc}/the_file>${cwd}/tmp/file-list

$bperl -e "set_global_maximum_concurrent_jobs(10)"

start_test

numvols=220
numjobs=20
maxbytes=2097152

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@output /dev/null
setdebug level=15 storage=File
@#setdebug level=200 client
@#setdebug level=100 director

END_OF_DATA

i=0
while [ $i -lt $numvols ] ; do
   cat <<END_OF_DATA >>${tmp}/bconcmds
label storage=File volume=TestVolume$i
update volume=TestVolume$i MaxVolBytes=${maxbytes}
END_OF_DATA
   i=$(($i + 1))
done

# Make one more big volume
cat <<END_OF_DATA >>${tmp}/bconcmds
label storage=File volume=TestVolume1000
@# start logging
@$out ${tmp}/log1.out
END_OF_DATA

i=0
while [ $i -lt ${numjobs} ] ; do
   cat <<END_OF_DATA >>${tmp}/bconcmds
run job=Simple yes
END_OF_DATA
   i=$(($i + 1))
done

cat <<END_OF_DATA >>${tmp}/bconcmds
wait
messages
END_OF_DATA

cat ${tmp}/bconcmds

run_bacula
check_for_zombie_jobs storage=File


my_error=0
i=0
while [ $i -lt ${numjobs} ] ; do
   rm -rf ${tmp}/bacula-restores
   mkdir -p ${tmp}/bacula-restores
   i=$(($i + 1))
   cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log2.out
list joblog jobid=%i
sql "select * from JobMedia WHERE JobId=%i;"

restore jobid=$i where=${tmp}/bacula-restores all storage=File done yes
wait
messages
END_OF_DATA
   run_bconsole
   ls -l ${tmp}/bacula-restores/${tmpsrc}/the_file
   md5=`md5sum ${tmp}/bacula-restores/${tmpsrc}/the_file | cut -d " " -f 1`
   if [ $the_file_md5 != $md5 ] ; then
      echo ERROR ERROR ERROR ERROR ERROR job $i
      my_error=1
   fi
done


check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
end_test

exit $my_error
